import away3d.audio.*;
import away3d.cameras.*;
import away3d.cameras.lenses.*;
import away3d.containers.*;
import away3d.core.base.*;
import away3d.core.clip.*;
import away3d.core.math.*;
import away3d.events.*;
import away3d.lights.*;
import away3d.loaders.*;
import away3d.materials.*;
import away3d.primitives.*;
import away3d.sprites.*;

import com.powerfl.frm.taf.*;

import flash.display.*;
import flash.media.*;
import flash.text.*;
import flash.utils.*;

final public function addRes( $key:String, $path:String, $ini:Object = null, ...$param ):void{
	var param:TAFparam;
	param = $paramGet( $param );
	orderPush( function():void{
		var call:Function;
		call = arguments.callee;
		if( treeCheck( $key ) ){
			TAFloader.LOAD( 'byte', $path, param, function( $data:ByteArray ):void{
				resGet( $data, function( $result:TAFres ):void{
					var key:*, alphaTemp:Array, iniText:String, ini:Object, subObject:Object;
					alphaTemp = [];
					for( key in $result.bitmap ){ 
						addTree( '@DATA', key, 'image', $result.bitmap[key], param );
						addTree( '@DATA', key+'@size', 'data', [$result.bitmap[key].width, $result.bitmap[key].height], null );
						if( key.substr( -7 ) === '@@ALPHA' ){
							alphaTemp[alphaTemp.length] = key;
						}
					}
					if( alphaTemp.length ){
						for each( key in alphaTemp ){
							SELF._trees[key.substring( 0, key.indexOf( '@@ALPHA' ) )].data = bitmapAPG_( tdata( key.substring( 0, key.indexOf( '@@ALPHA' ) ) ), tdata( key ) );
						}
						treeRemove_( alphaTemp );
					}
					for( key in $result.byte ){ 
						addTree( '@DATA', key, 'byte', $result.byte[key], param );
					}
					for( key in $result.sound ){
						addTree( '@DATA', key, 'sound', poolPullSound( $result.sound[key] ), param );
					}
					iniText = '';
					for( key in $result.string ){
						if( key.substr( 0, 2 ) === '@@' ){
							addTree( '@DATA', key, 'TAFS', $result.string[key], param );
//							if( $result.string[key] ){
//								iniText += $result.string[key] + '\r\n';
//							}
						}else{
							addTree( '@DATA', key, 'text', $result.string[key], param );
						}
					}
//					if( iniText ){
//						ini = INI( iniText );
//						addTAFscriptPre( ini );
//						if( $ini ){
//							orderBranchAdd( function():void{
//								for( key in ini ){
//									if( ini[key] ){
//										if( $ini[key] ){
//											error( '이미 선언된 항목을 재선언 했습니다. - ' + key );
//										}else{
//											$ini[key] = ini[key];
//										}
//									}
//								}
//							} );
//						}else{
//							addTAFscriptEnd( ini );
//						}
//					}
					orderCollect( call );
					poolCollectParam( param );
				} );
			} );
		}else{
			orderCollect( call );
			poolCollectParam( param );
		}
	} );
}
final public function addTAFS( $key:String, $path:*, ...$param ):void{
	var i:int, j:int, ini:Object;
	ini = {};
	if( treeCheck( $key ) ){}else{
		return;
	}
	if( $path is Array ){}else{
		$path = [$path];
	}
	for( i = 0, j = $path.length ; i < j ; ++i ){
		addText( '$addIni' + i.toString(), $path[i] );
	}
	//파일을 병합하여 ini생성
	orderAdd( null, function():void{
		var i:int, txt:String, key:*, removeList:Array;
		txt = '';
		removeList = [];
		for( i = 0 ; i < j ; ++i ){
			key = '$addIni' + i.toString();
			removeList[i] = key;
			txt += tdata( key ) + '\r\n';
		}
		treeRemove_( removeList );
		ini.ini = INI( txt );
	} );
	addTAFscriptPre( ini );
	addTAFscriptEnd( ini );
}
private function addTAFscriptPre( $ini:Object ):void{
	orderBranchAdd( function():void{
		var i:int, j:int, key:*, key2:*, ini:Object, initScript:Array, param:TAFparam, removeList:Array, temp:Object;
		$ini = $ini.ini;
		for( key in $ini ){
			temp = $ini[key];
			switch( key.toLowerCase() ){
			case'@res':
				for( key2 in temp ){
					addRes( key2, temp[key2], $ini );
				}
				$ini[key] = null;
				break;
			case'@timeline':
				for( key2 in temp ){
					tweenLineData( key2, arrayCastNumber( arrayEvenIndex( temp[key2] ), false ), arrayOddIndex( temp[key2] ) );
				}
				$ini[key] = null;
				break;
			case'@template':
				for( key2 in temp ){
					if( tdata( '@TEMPLATE' )[key2] ){
						error( '이미 존재하는 template. - ' + key2 );
					}else{
						tdata( '@TEMPLATE' )[key2] = temp[key2];
					}
				}
				$ini[key] = null;
				break;
			case'@state':
				for( key2 in temp ){
					if( tdata( '@STATE' )[key2] ){
						error( '이미 존재하는 state. - ' + key2 );
					}else{
						tdata( '@STATE' )[key2] = temp[key2];
						tdata( '@STATEVAR' )[key2] = temp[key2][0];
					}
				}
				$ini[key] = null;
				break;
			case'@bind':
				for( key2 in temp ){
					bindState_( key2, temp[key2] );
				}
				$ini[key] = null;
				break;
			case'@sound':
				for( key2 in temp ){
					addSound( key2, temp[key2]  );
				}
				$ini[key] = null;
				break;
			case'@image':
				for( key2 in temp ){
					addImage( key2, temp[key2] );
				}
				$ini[key] = null;
				break;
			case'@global':
				for( key2 in temp ){
					if( tdata( '@global' )[key2] === undefined ){
						tdata( '@global' )[key2] = temp[key2];
					}else{
						error( '이미 global에 정의된 변수입니다. - ' + key2 + ' = ' + tdata( '@global' )[key2] );
					}
				}
				$ini[key] = null;
				break;
			}
		}
	} );
}
private function addTAFscriptEnd( $ini:Object ):void{
	orderAdd( null, function():void{
		var key:*, key2:*, temp:*, tree:TAFtree;
		$ini = $ini.ini;
		for( key in $ini ){
			temp = $ini[key];
			if( key === '@batch' ){
				for( key2 in temp ){
					flowAddMacro_( key2, scriptPre( [temp[key2]] ) );
				}
			}else if( key.substr( 0, 2 ) === '@@' ){
				flowAdd_( key.substr( 2 ) );
				addTree( '@script', '@' + key + '@script', 'TS', scriptPre( $ini[key] ), null );
			}else if( key.charAt(0) === '@' ){
				addTree( '@local', '@' + key, 'data', $ini[key], null );
			}else{
				tree = treeGet( key );
				if( tree && tree.type === 'image' ){
					addMapEnd( tree.data, $ini[key] );
				}
			}
			$ini[key] = null;
		}
	} );
}
